package com.tanhua.server.controller;

import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.CommentCountVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {
    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private TanhuaService tanhuaService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 更新用户头像
     */
    @PostMapping("/header")
    public ResponseEntity head(MultipartFile headPhoto) throws IOException {
        //2、向userinfo中设置用户id
        //3、调用service
        userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        return ResponseEntity.ok(null);
    }

    /**
     * 修改手机号，发送短信验证码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String phone = UserHolder.getMobile();
        userService.sendMsg(phone, Constants.UPDATE_MOBILE_SMS_CODE);
        return ResponseEntity.ok(null); //正常返回状态码200
    }

    /**
     * 更新手机号校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        //1、调用map集合获取请求参数
        String code = (String) map.get("verificationCode");
        String phone = UserHolder.getMobile();
        //2、更新手机号校验验证码
        Map result = userInfoService.checkVerificationCode(phone,code);
        //3、构造返回

        return ResponseEntity.ok(result);
    }

    /**
     * 更新手机号
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        User user = new User();
        user.setId(UserHolder.getUserId());
        user.setMobile(phone);
        userService.updateMobile(user);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询互动数据
     */
    @GetMapping("/counts")
    public ResponseEntity counts() {
        CommentCountVo countVo = this.tanhuaService.getCommentCount();
        return ResponseEntity.ok(countVo);
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity counts(@PathVariable("type") Integer type,
                                 @RequestParam(defaultValue = "1")  Integer page,
                                 @RequestParam(defaultValue = "10") Integer pagesize,
                                 String nickname) {
        PageResult pageResult = this.tanhuaService.getFriendResult(type,nickname,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 喜欢-取消
     */
    @DeleteMapping("/like/{userId}")
    public ResponseEntity unLike(@PathVariable("userId") Long userId) {
        this.tanhuaService.unLike(userId);
        return ResponseEntity.ok(null);
    }

    /**
     * 粉丝-喜欢
     */
    @DeleteMapping("/fans/{userId}")
    public ResponseEntity fanslike(@PathVariable("userId") Long userId) {
        this.tanhuaService.fanslike(userId);
        return ResponseEntity.ok(null);
    }
}
